﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using TimKen.BussLogic;
using TimKen.DataEntity;
using System.Threading;
using System.Globalization;

namespace TimKen.Web
{
    public partial class LoginWebForm1 : TimKen.App.WebPage
    {
        TimKen.BussLogic.UserAccountManager ua = new TimKen.BussLogic.UserAccountManager();
        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                BindResources();
            }
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (Session["CheckCode"] != null && Session["CheckCode"].ToString() == txtCheckCode.Text.Trim().ToLower())
            {
                try
                {
                    UserS u = UserFacade.Login(username.Text, password.Text);
                    if (u != null)
                    {
                        TimKen.DataEntity.UserAccount admin = ua.GetModel(u.UserName);
                        if (admin.RoleID == 1 || admin.RoleID == 0)
                        {
                            Session["Admin"] = u;
                        }

                        if (ddlLanguage.SelectedIndex == 0)
                        {
                            CultureInfo culture = new CultureInfo("zh-CN");
                            System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
                            System.Threading.Thread.CurrentThread.CurrentCulture = culture;
                        }
                        else
                        {
                            CultureInfo culture = new CultureInfo("en-US");
                            System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
                            System.Threading.Thread.CurrentThread.CurrentCulture = culture;
                        }
                    }
                    Response.Redirect("/admin/index.aspx");
                }
                catch (ArgumentNullException)
                {
                    Msg.Text = TimKen.Localization.LocalizationUtility.GetText("UserNameOrPasswordInvalid");
                }
            }
            else
            {
                Msg.Text = TimKen.Localization.LocalizationUtility.GetText("InvalidCheckCode");
            }
        }

        private void SetLanguage(string language)
        {                          
            Session.Add("lan", language);
            HttpCookie cookie = new HttpCookie("Language");
            DateTime dt = DateTime.Now;//定义时间对象
            TimeSpan ts = new TimeSpan(0, 20, 0);
            cookie.Expires.Add(ts);
            cookie.Value = language;
            HttpContext.Current.Response.AppendCookie(cookie);
            CultureInfo culture = new CultureInfo(language);
            System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
            System.Threading.Thread.CurrentThread.CurrentCulture = culture;

            Response.Redirect(Request.Path);
        }         
        protected void BindResources()
        {
            btnLogin.Text = TimKen.Localization.LocalizationUtility.GetText("EnterManagementCenter");
            ddlLanguage.Items.Clear();

            ListItem li_ZH = new ListItem("简体中文", "1");
            ListItem li_EN = new ListItem("English", "2");

            string language = Request.Cookies["Language"] == null ? null : Request.Cookies["Language"].Value;
            if (language == "en-US")
            {
                li_EN.Selected = true;
            }

            ddlLanguage.Items.Add(li_ZH);
            ddlLanguage.Items.Add(li_EN);
        }


        ///// <summary>
        ///// 获取多语言
        ///// </summary>
        ///// <param name="key"></param>
        ///// <returns></returns>
        //public string GetLangString(string key)
        //{
        //    return TimKen.Localization.LocalizationUtility.GetText(key);
        //}

        protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlLanguage.SelectedIndex == 0)
            {
                SetLanguage("zh-CN");
            }
            else
            {
                SetLanguage("en-US");
            }
        }
    }
}